package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.dao.RoleDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult pagequery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<Role> page = roleDao.selectByConition(queryString);

        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public List<Integer> findPermissionById(Integer id) {
        return roleDao.findById(id);

    }

    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        roleDao.add(role);
        this.setRoleAndPermission(role, permissionIds);
        this.setRoleAndMenus(role, menuIds);
    }

    @Override
    public List<Menu> findMenusAll() {
        return menuDao.findAllMenus();
    }

    @Override
    public void deleteById(Integer id) {
        List<Integer> rolePermissionByRoleId = roleDao.findRolePermissionByRoleId(id);
        List<Integer> menuByRoleId = roleDao.findMenuByRoleId(id);
        List<Integer> userByRoleId = roleDao.findUserIdsByRoleId(id);
        if (menuByRoleId.size() > 0) {
            throw new RuntimeException("当前角色有关联菜单,请解除关系后再试");
        }
        if (rolePermissionByRoleId.size() > 0) {
            throw new RuntimeException("当前角色有关联权限,请解除关系后再试");
        }
        if (userByRoleId.size() > 0) {
            throw new RuntimeException("当前角色有关联用户,请解除关系后再试");
        }
        roleDao.deletePermissionByRoleId(id);
        roleDao.deleteMenuByRoleId(id);
        roleDao.deleteRoleById(id);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleId) {
        return roleDao.findMenuByRoleId(roleId);
    }

    @Override
    public List<Integer> findRolePermissionByRoleId(Integer roleId) {
        return roleDao.findRolePermissionByRoleId(roleId);
    }

    @Override
    public List<Permission> findAllPermissions() {
        return roleDao.findAllPermissions();
    }

    @Override
    public boolean edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        boolean result = false;
        result = roleDao.edit(role);
        // 先把相关的信息删除掉  再进行更新
        roleDao.deleteMenuByRoleId(role.getId());
        roleDao.deletePermissionByRoleId(role.getId());
        for (Integer permissionId : permissionIds) {
            result = roleDao.updateRoleAndPermission(role.getId(), permissionId);
        }
        for (Integer menuId : menuIds) {
            result = roleDao.updateRoleAndMenu(role.getId(), menuId);
        }
        return result;
    }


    public void setRoleAndPermission(Role role, Integer[] permissionIds) {
        //重新建立当前检查组和检查项的关联关系
        Integer roleId = role.getId();
        if (permissionIds != null && permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                roleDao.setRoleAndPermission(roleId, permissionId);
            }
        }
    }

    public void setRoleAndMenus(Role role, Integer[] menusIds) {
        //重新建立当前检查组和检查项的关联关系
        Integer roleId = role.getId();
        if (menusIds != null && menusIds.length > 0) {
            for (Integer menuId : menusIds) {
                roleDao.setRoleAndMenus(roleId, menuId);
            }
        }
    }
}
